<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { bus } from '@/utils/mitt'
import { getUserInfo, editAdminAPI } from '@/api/userInfo'
import { ElMessage } from 'element-plus'
const title = ref()
bus.on('editId', async (id: number) => {
  const res = await getUserInfo(id)
  formData.id = res.data.id
  formData.account = res.data.account
  formData.name = res.data.name
  formData.sex = res.data.sex
  formData.email = res.data.email
  formData.department = res.data.department
  title.value = '编辑管理员信息'
})

interface FormData {
  id: number
  account: number
  name: string
  sex: string
  email: string
  department: string
}

const formData: FormData = reactive({
  id: null,
  account: null,
  name: '',
  sex: '',
  email: '',
  department: ''
})

const rules = reactive({
  account: [
    {
      required: true,
      message: '请输入管理员账号',
      min: 6,
      max: 12,
      trigger: 'blur'
    }
  ],
  name: [
    {
      required: true,
      message: '请输入管理员姓名',
      trigger: 'blur'
    }
  ],
  sex: [
    {
      required: true,
      message: '请输入管理员性别',
      trigger: 'blur'
    }
  ],
  email: [
    {
      required: true,
      message: '请输入管理员邮箱',
      min: 6,
      max: 12,
      trigger: 'blur'
    }
  ],
  department: [
    {
      required: true,
      message: '请输入管理员部门',
      trigger: 'blur'
    }
  ]
})

// 弹窗开关
const dialogFormVisible = ref(false)

// 打开创建管理员的弹窗
const open = () => {
  dialogFormVisible.value = true
}
defineExpose({
  open
})
const emit = defineEmits(['success'])
const editAdmin = async () => {
  try {
    if(!formData.name ||!formData.sex ||!formData.email ||!formData.department){
      ElMessage.error('请输入完整信息')
      return
    }
    const res = await editAdminAPI(formData)
    if (res.status === 200) {
      ElMessage.success(res.data.message)
      dialogFormVisible.value = false
      emit('success')
    }
  } catch (error) {
    ElMessage.error(error)
  }
}
</script>
<template>
  <el-dialog
    v-model="dialogFormVisible"
    title="编辑管理员信息"
    destroy-on-close
    width="600px"
    align-center
    draggable
  >
    <div class="dialog-content">
      <el-form
        ref="ruleFormRef"
        :model="formData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="账号" prop="account">
          <el-input
            v-model="formData.account"
            disabled
            :value="formData.account"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="formData.sex" placeholder="请选择性别">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-select v-model="formData.department" placeholder="请选择部门">
            <el-option label="总裁办" value="总裁办" />
            <el-option label="项目部" value="项目部" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="editAdmin"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<style lang="less" scoped>
.dialog-content {
  display: flex;
  padding: 20px 30px;
}

:deep(.el-form-item) {
  margin: 30px;
}

</style>
